package com.tanjun.gateway.gatewayservice.service.impl;

import com.tanjun.common.enums.user.UserTypeEnum;
import com.tanjun.common.model.LoginUserInfo;
import com.tanjun.common.model.R;
import com.tanjun.gateway.gatewayservice.service.UserLoginService;
import com.tanjun.gateway.gatewayservice.service.request.GetUser;
import com.tanjun.gateway.gatewayservice.service.response.UserPwdInfo;
import com.tanjun.userservice.apiservice.api.AdminUserApi;
import com.tanjun.userservice.apiservice.response.adminuser.admin.AdminUserBaseResponse;
import com.tanjun.userservice.apiservice.response.adminuser.api.AdminUserResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
public class UserLoginServiceImpl implements UserLoginService {
    @Autowired
    @Lazy
    private AdminUserApi adminUserApi;

    /**
     * 获取用户id和密码
     *
     * @param paramVo 用户
     * @return 用户id和密码
     */
    @Override
    public UserPwdInfo getUserPwd(GetUser paramVo) {
        switch (paramVo.getUserRoleType()){
            case ADMIN:
                R<AdminUserBaseResponse> r=adminUserApi.getAdminBaseInfoByKey(paramVo.getUserKey());
                if(r.isSuccess()){
                    UserPwdInfo userPwdInfo=new UserPwdInfo();
                    userPwdInfo.setId(r.getData().getUserId());
                    userPwdInfo.setPwd(r.getData().getPwd());
                    userPwdInfo.setSalt(r.getData().getSalt());
                    userPwdInfo.setEnabled(r.getData().getEnabled());
                    return userPwdInfo;
                }

        }
        return null;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @Override
    public LoginUserInfo getLoginUserInfo(UserTypeEnum userType, Long userId) {
        String userKey="loginInfo:"+userType.getCode()+":"+userId;
        LoginUserInfo loginUserInfo= null;
        if(loginUserInfo==null) {
            switch (userType) {
                case ADMIN:
                    R<AdminUserResponse> r = adminUserApi.getUserBaseInfo(userId);
                    if (r.isSuccess()) {
                        loginUserInfo=new LoginUserInfo();
                        loginUserInfo.setUserId(r.getData().getUserId());
                        loginUserInfo.setUserCode(r.getData().getUserName());
                        loginUserInfo.setUserName(r.getData().getRealName());
                        loginUserInfo.setUserType(UserTypeEnum.ADMIN);
                        loginUserInfo.setAdminUserTypeEnum(r.getData().getUserType());
                        loginUserInfo.setVendorId(r.getData().getVendorId());
                    }
                case MEM:
                    break;
            }
            //cloudCacheManager.saveValue(userKey, loginUserInfo, 60 * 5L);
        }
        return loginUserInfo;
    }


}
